﻿@using Db8.Data
@using Db8.ViewModels
@model IEnumerable<ClaimViewModel>
@{
    ViewBag.Title = "Claim Index";
}
<section class="content">
    <section class="view-header">
        <div style="float:right">@Html.Partial("_Navigation")</div>
        <div style="float:left">@Html.Partial("_Filter")</div>
        <span class="create-link" data-baseurl="@Url.Action("CreateRoot", new { counterTarget = 0 })">
        @if (User.IsInRole(Db8.Db8Roles.CONTENT_ADMINISTRATOR))
        {
            @Html.ActionLink("Enter a New Claim", "CreateRoot", new { counterTarget = 0 })
        }
        &nbsp;
        </span>
    </section>
    <section class="view-body">
        @foreach (var item in Model)
        {
            @Html.Partial("_ClaimView", item)
        }
    </section>
    @if (string.IsNullOrEmpty(Request["filter"]))
    {
    <section class="view-footer">
        <div class="claim-box-more" load-action="@Url.Action("IndexMore", "Claim")" load-arg-filter="@Html.Raw(HttpUtility.UrlEncode(Request["filter"]))">
            More
        </div>
    </section>
    }
</section>
@section scripts {
    <script type="text/javascript">
        (function ($) {
            var url = $(".create-link").attr("data-baseurl") + "&filter=" + encodeURIComponent($("#filter").val());
            $(".create-link a").attr("href", url);

            $("#filter").change(function () {
                var url = $(".create-link").attr("data-baseurl") + "&filter=" + encodeURIComponent($(this).val());
                $(".create-link a").attr("href", url);
            });

            var PAGE_SIZE = 10;
            var skip0 = $(".view-body div.claim-box").length;
            if (skip0 < PAGE_SIZE)
                $("div.claim-box-more").hide();
            if (skip0 >= PAGE_SIZE) {
                // more button
                $("div.claim-box-more").click(function () {
                    $this = $(this);
                    var skip = $(".view-body div.claim-box").length;
                    $this.addClass("claim-box-more-loading");
                    var loadAction = $this.attr("load-action") + "?filter=" + $this.attr("load-arg-filter") + "&skip=" + skip;
                    var container = $(".view-body").first();
                    var loadContainer = $("<div/>")
                    loadContainer.load(loadAction, function (responseText, textStatus) {
                        if (textStatus != "success") {
                            alert("An error occurred: " + textStatus);
                            $this.removeClass("claim-box-more-loading");
                        } else {
                            if ($(loadContainer).children().length < PAGE_SIZE)
                                $("div.claim-box-more").hide();
                            if ($(loadContainer).children().length > 0) {
                                container.append($(loadContainer).children());
                                // reapply UI init js
                            }
                            $this.removeClass("claim-box-more-loading");
                        }
                    });
                });
            }


        } (jQuery));
    </script>
}